/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using Xceed.Silverlight.Data.Stats;
using System.Collections.Generic;
using System.Linq;

namespace Xceed.Silverlight.Data
{
  internal class GroupEnumerator : AsyncParentEnumerator<DataPath>
  {
    private static readonly Guid JumpMoveAndFetchCallId = DataUtils.CreateCallId();

    public GroupEnumerator( IAsyncEnumerator<DataPath> childEnumerator, GroupHelper groupHelper )
      : base( null )
    {
      GroupLevelEnumerator groupLevelEnumerator = new GroupLevelEnumerator( childEnumerator, groupHelper );
      GroupMoveEnumerator groupMoveEnumerator = new GroupMoveEnumerator( groupLevelEnumerator );
      GroupContextEnumerator groupStatEnumerator = new GroupContextEnumerator( groupMoveEnumerator );
      this.ChildEnumerator = groupStatEnumerator;
    }

    #region GroupContextEnumerator

    private GroupContextEnumerator ContextEnumerator
    {
      get
      {
        return ( GroupContextEnumerator )this.ChildEnumeratorTyped;
      }
    }

    #endregion GroupMoveEnumerator


    #region GroupMoveEnumerator

    private GroupMoveEnumerator MoveEnumerator
    {
      get
      {
        return ( GroupMoveEnumerator )this.ContextEnumerator.ChildEnumeratorTyped;
      }
    }

    #endregion GroupMoveEnumerator

    #region GroupLevelEnumerator

    private GroupLevelEnumerator LevelEnumerator
    {
      get
      {
        return ( GroupLevelEnumerator )this.MoveEnumerator.ChildEnumerator;
      }
    }

    #endregion GroupLevelEnumerator

    #region CachedPathProvider Property

    internal Func<DataPath, DataPath> CachedPathProvider
    {
      get
      {
        return this.LevelEnumerator.CachedPathProvider;
      }

      set
      {
        this.LevelEnumerator.CachedPathProvider = value;
      }
    }

    #endregion CachedPathProvider Property

    #region GroupMoveEnumerator Type

    private class GroupMoveEnumerator : MoveConverterEnumerator<DataPath>, IGroupEnumerator
    {
      public GroupMoveEnumerator( GroupLevelEnumerator childEnumerator )
        : base( childEnumerator )
      {
      }

      public GroupHelper GroupHelper
      {
        get
        {
          return this.GroupLevelEnumerator.GroupHelper;
        }
      }

      private GroupLevelEnumerator GroupLevelEnumerator
      {
        get
        {
          return ( GroupLevelEnumerator )this.ChildEnumerator;
        }
      }

      protected override bool ConvertMoveForRequest( JumpMoveAndFetchParameters parameters )
      {
        //If the move is approximate, do not fetch it, pass the call directly to the child enumerator.
        //If no modifications are made to the content, move will be precise anyway.
        return parameters.ExpectedMoveCount != 0
          && !parameters.ApproximateMove
          && this.GroupLevelEnumerator.AddOrRemoveItems;
      }

      public IAsyncResult BeginGroupItemCount( DataPath groupPath, AsyncCallback userCallback, object asyncState )
      {
        return this.GroupLevelEnumerator.BeginGroupItemCount( groupPath, userCallback, asyncState );
      }

      public EstimateInt64 EndGroupItemCount( IAsyncResult asyncResult )
      {
        return this.GroupLevelEnumerator.EndGroupItemCount( asyncResult );
      }

      #region IGroupEnumerator Members


      public IAsyncResult BeginGetBaseItems( JumpMoveAndFetchParameters parameters, AsyncCallback userCallback, object asyncState )
      {
        return this.GroupLevelEnumerator.BeginGetBaseItems( parameters, userCallback, asyncState );
      }

      public JumpMoveAndFetchResult<DataPath> EndGetBaseItems( JumpMoveAndFetchAsyncResult jumpMoveAndFetchAsyncResult )
      {
        return this.GroupLevelEnumerator.EndGetBaseItems( jumpMoveAndFetchAsyncResult );
      }

      #endregion
    }

    #endregion GroupMoveEnumerator Type
  }

}
